package org.megha.blog.example.part10;

import java.util.ArrayList;
import java.util.List;

import org.megha.blog.example.part5.Circle;
import org.megha.blog.example.part5.Shape;
import org.megha.blog.example.part5.Square;

public class ArrayListExample {

	public static void main(String args[]) {
		// create a list backed by an array
		List<Shape> shapes = new ArrayList<Shape>();

		// add four shapes to the list
		shapes.add(new Circle(4));
		shapes.add(new Circle(8));
		shapes.add(new Square(6));
		shapes.add(new Square(8));

		// looping over and printing all elements in the list
		for (int i = 0; i < shapes.size(); i++) {
			System.out.println(shapes.get(i));
		}

		// alternate (and better) way of looping over the list
		// note: this internally gets an iterator and loops over elements
		for (Shape s : shapes) {
			System.out.println(s);
		}

		boolean isListEmpty = shapes.isEmpty(); // false
		Shape firstElement = shapes.get(0);
		boolean containsFirstElement = shapes.contains(firstElement); // true

		// delete everything from the list
		shapes.clear();

		isListEmpty = shapes.isEmpty(); // true
		containsFirstElement = shapes.contains(firstElement); // false
	}
}
